
/* JavaScript content from js/issue/addIssue.js in folder common */
/**
 * 
 */


var issueForm = {};
var issue_id = null;
$(document).on("pagebeforeshow", "#issueForm", function(event) {
	WL.Logger.debug("IssueShow::on::pagebeforeshow");
	busyIndicator.hide();

	issueForm.makeIssueForm(sessionStorage.selectedProjectId);
});

issueForm.makeIssueForm = function(project_id) {
	busyIndicator = new WL.BusyIndicator("content", {
		text : "Loading..."
	});
	busyIndicator.show();
	WL.Logger.debug("IssueForm::invokeProcedure::GetIssueFormEnum");
	var invocationData = {
		adapter : "Redmine",
		procedure : "getIssueFormEnum",
		parameters : [project_id]
	};
	// busyIndicator.show();
	WL.Client
			.invokeProcedure(
					invocationData,
					{
						onSuccess : function(response) {

							if (response.invocationResult.statusCode == 200) {
								WL.Logger
										.debug("IssueShow::invokeProcedure::Successful");
								issueForm.renderIssueForm(response.invocationResult);
							} else
								alert("Invoke getProjects Procedure Failure.StatusCode!=200. Response :: "
										+ JSON.stringify(error));

						},
						onFailure : function(response) {
							if (response.invocationResult.authStatus == "required") {
								customAuthenticatorRealmChallengeHandler
										.handleChallenge(response);
							} else {
								WL.Logger
										.debug("ProjectIssues::invokeProcedure::Failure. Error = "
												+ JSON.stringify(error));
								alert("Invoke getProjects Procedure Failure. Response :: "
										+ JSON.stringify(error));
							}
						}
					});
	busyIndicator.hide();
	return null;
};

issueForm.renderIssueForm = function (data){
	$('#issueProjectTitle').html(project_list[sessionStorage.selectedProjectId].name);
	$("#issueFormView").find('select[field]').each( function(i,selectTag){
//		WL.Logger.debug(selectTag.toString());
		$.each(data[$(selectTag).attr("field")], function (j,item){
			$("<option>").val(item.id).html(item.name).appendTo($(selectTag));
			/*
			if (item.is_default==true){
				$("<option>").val(item.id).html(item.name).attr("selected","selected").appendTo($(selectTag));
			}
			else {
				if (j==0)
					$("<option>").val(item.id).html(item.name).attr("selected","selected").appendTo($(selectTag));
				else $("<option>").val(item.id).html(item.name).appendTo($(selectTag));
			}
			*/
		});
		

		
	});
	
};

issueForm.postIssue = function(issue){
	WL.Logger.debug("IssueForm::invokeProcedure::PostIssue");
	WL.Logger.debug(issue);
	var invocationData = {
		adapter : "Redmine",
		procedure : "createIssue",
		parameters : [issue]
	};
	// busyIndicator.show();
	WL.Client
			.invokeProcedure(
					invocationData,
					{
						onSuccess : function(response) {

							if (response.invocationResult.statusCode == 201) {
								WL.Logger
										.debug("IssueForm::invokeProcedure::PostIssue::Successful");
								sessionStorage.selectedIssueId = response.invocationResult.issue.id  ;
								$.mobile.changePage("IssueShow.html", {
									    transition: 'flip',changeHash : false,
									    reverse: true
									  });
							} else
								alert("Invoke post Procedure Failure. Response :: "
										+ JSON.stringify(error));

						},
						onFailure : function(response) {
							if (response.invocationResult.authStatus == "required") {
								customAuthenticatorRealmChallengeHandler
										.handleChallenge(response);
							} else {
								WL.Logger
										.debug("ProjectIssues::invokeProcedure::Failure. Error = "
												+ JSON.stringify(error));
								alert("Invoke getProjects Procedure Failure. Response :: "
										+ JSON.stringify(error));
							}
						}
					});
};

$(document).on("pageshow", "#issueForm", function() {
	$('#issueSaveButton').on('click', function (e) {
		  e.preventDefault();
	/*	  var page = document.referrer;
	*/
  		  var issueObject = {
				 issue : {}
		  };
  		issueObject.issue.project_id = sessionStorage.selectedProjectId ;
  		$("#issueFormView").find('select , input , textarea').each( function(){

  			issueObject.issue[this.name] = $(this).val();
  			});

		  var issueJsonString  = JSON.stringify(issueObject);
		  issueForm.postIssue(issueJsonString);

		});
});